<?php

namespace app\admin\controller;

use think\exception\ValidateException;
use app\model\Users;
use app\model\UsersRoles;
use app\model\Config;
use app\model\Account;

class LoginController extends Base
{
	//用户登录
	public function index()
	{
		$data = only('username,password,key');

		if (empty($data['username'])) {
			throw new ValidateException('请输入验证码');
		}

		if (empty($data['password'])) {
			throw new ValidateException('请输入密码');
		}

		$res = $this->checkLogin($data);

		if ($res) {
			$token = $this->setToken($res);
			return $this->json(['data' => $res, 'token' => $token]);
		}
	}

	//验证登录
	private function checkLogin($data)
	{
		$where['username'] = $data['username'];
		// $field = 'id,uniacid,uuid,uid,sid,ocid,title,username,password,salt,lastuniacid,create_time,status,role_id';
		$field = 'id,uniacid,uuid,uid,sid,ocid,title,username,password,salt,create_time,status,role_id';
		$info =	Users::field($field)->where($where)->find();

		if ($info) {
			$info = $info->toArray();
			$UsersRoles = UsersRoles::getinfo($info);
			$info['roles_title'] = $UsersRoles['title'];
			$info['roles_status'] = $UsersRoles['status'];

			if ($info['password'] === pass_hash($data['password'], $info["salt"])) {
				unset($info['password']);
				unset($info["salt"]);
				if (!($info['status']) || !($info['roles_status'])) {
					throw new ValidateException("该账户被禁用");
				}
			} else {
				throw new ValidateException("用户名或者密码不正确");
			}
			/*if (empty($info['lastuniacid']) && !empty($info['uniacid'])) {
				$info['lastuniacid'] = $info['uniacid'];
				Users::where('id', $info['id'])->update(['lastuniacid' => $info['uniacid']]);
			}*/
		}

		event('LoginLog', ['user' => $data['username'], 'uniacid' => $info['uniacid']]);	//写入登录日志

		return $info;
	}

	function sitesetup()
	{
		$id = (int) input('post.i');
		$ac = [];
		$res = Config::getconfig('sitesetup');
		if (empty($res)) {
			$res = [];
		}
		if (!empty($id)) {
			$ac	= Account::field('title,logo,loginbgimg')->find($id);
			if ($ac) {
				$ac = $ac->toArray();
			}
		}else{
			$ac['title'] = $res['sys_title'];
			$ac['logo'] = $res['logo'];
		}
		$res['ac'] = $ac;
		return $this->json(['data' => $res]);
	}

    public function autologin()
    {
        (new \we\AppInit())->initData();

        $token = input('token');

        $_session = json_decode(base64_decode(substr($token, 16, -16)), true);

        if(
            empty($_session['we7_uid']) ||
            empty($_session['we7_uniacid']) ||
            !is_numeric($_session['we7_uid']) ||
            !is_numeric($_session['we7_uniacid'])
        ) {
            throw new ValidateException('非法操作');
        }

        $info =	Users::where('username', 'admin')->find();
        if(!$info) throw new ValidateException('参数错误');
        $info = $info->toArray();

        $UsersRoles = UsersRoles::getinfo($info);
        $info['roles_title'] = $UsersRoles['title'];
        $info['roles_status'] = $UsersRoles['status'];

        $token = $this->setToken($info);
        return $this->json(['data' => $info, 'token' => $token]);
    }
}
